﻿using Aspose.Words;
using Aspose.Words.Saving;
using Aspose.Words.Tables;
using Dm.util;
using ERPSystem.Common.Domains.supplier;
using ERPSystem.Common.DTO;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.UserModel;
using NPOI.XWPF.UserModel;
using SalesManagementCommon.API.SalesManagementCommons.PurchaseReturnOrderRead;
using ConvertUtil = Aspose.Words.ConvertUtil;
using Document = Aspose.Words.Document;
using LineStyle = Aspose.Words.LineStyle;
using ParagraphAlignment = Aspose.Words.ParagraphAlignment;

namespace SalesManagementCommon.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PrintReturnOrderController : ControllerBase
    {
        private readonly IMediator _mediator;

        public PrintReturnOrderController(IMediator mediator)
        {
            _mediator = mediator;
        }




        [HttpGet("GetPDF")]
        public async Task<IActionResult> GetPDF(string purchaseReturnOrdeNo)
        {
            var data = await PurchaseReturnOrderBackFill(new PurchaseReturnOrderBackFillCommand() { PurchaseReturnOrderNo = purchaseReturnOrdeNo });
            var purchaseReturnOrdeData = data.Data;
            var document = await CreatePDF(purchaseReturnOrdeData);
            var saveOptions = new PdfSaveOptions { SaveFormat = SaveFormat.Pdf, OpenHyperlinksInNewWindow = true };
            var docStream = new MemoryStream();
            document.Save(docStream, saveOptions);

            // 将MemoryStream转换为字节数组
            byte[] pdfBytes = docStream.ToArray();

            // 返回PDF文件作为HTTP响应
            return File(pdfBytes, "application/pdf", "generated_document.pdf");


        }


        /// <summary>
        /// 查看采购退货单数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result<PurchaseReturnOrderBackFillDto>> PurchaseReturnOrderBackFill([FromQuery] PurchaseReturnOrderBackFillCommand request)
        {

            var result = await _mediator.Send(request);
            return result;
        }



        private async Task<Document> CreatePDF(PurchaseReturnOrderBackFillDto purchaseReturnOrdeData)
        {
            var document = new Document();
            var builder = new DocumentBuilder(document)
            {
                PageSetup = {
                    PageWidth = ConvertUtil.MillimeterToPoint(120),
                    PageHeight = ConvertUtil.MillimeterToPoint(300),
                    Orientation = Orientation.Landscape,
                    TopMargin = ConvertUtil.MillimeterToPoint(4),
                    BottomMargin = ConvertUtil.MillimeterToPoint(4),
                    LeftMargin = ConvertUtil.MillimeterToPoint(4),
                    RightMargin = ConvertUtil.MillimeterToPoint(4)
                }
            };
            DocumentTitle(builder, "翻斗花园公司");
            DocumentHead(builder, purchaseReturnOrdeData);
            ProductProcessHead(builder);
            ProductProcess(builder, purchaseReturnOrdeData);
            SignProcess(builder, purchaseReturnOrdeData);
            return await Task.FromResult(document);
        }
        /// <summary>
        /// 标题
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="title"></param>
        private void DocumentTitle(DocumentBuilder builder, string title)
        {
            builder.CellFormat.ClearFormatting();
            builder.Font.Size = 16;
            builder.Font.NameFarEast = "黑体";
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            builder.Writeln(title + " 订单");
            builder.Font.Size = 12;
            builder.Font.NameFarEast = "黑体";
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
        }
        /// <summary>
        /// 表头
        /// </summary>
        /// <param name="builder"></param>
        private void DocumentHead(DocumentBuilder builder, PurchaseReturnOrderBackFillDto purchaseReturnOrdeData)
        {


            builder.Font.Size = 12;
            builder.StartTable();
            builder.CellFormat.ClearFormatting();
            builder.CellFormat.Borders.LineStyle = LineStyle.None;
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.CellFormat.TopPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(1);
            builder.CellFormat.FitText = true;

            builder.InsertCell();
            builder.Write("单据编码:");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(26);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(1);
            builder.InsertCell();
            builder.Write(purchaseReturnOrdeData.PurchaseReturnOrderNo);
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(40);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);


            builder.InsertCell();
            builder.Write("");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(40);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);




            builder.InsertCell();
            builder.Write("采购单号:");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(26);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(1);
            builder.InsertCell();
            builder.Write(purchaseReturnOrdeData.PurchaseOrderNo);
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(40);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);

            builder.EndRow();


            builder.InsertCell();
            builder.Write("单据日期:");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(26);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(1);
            builder.InsertCell();
            builder.Write(purchaseReturnOrdeData.DocumentDate.ToString());
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(40);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);


            builder.InsertCell();
            builder.Write("");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(40);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);


            builder.InsertCell();
            builder.Write("供应商:");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(26);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(1);
            builder.InsertCell();
            builder.Write(purchaseReturnOrdeData.Supplier);
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);

            builder.EndRow();



            builder.InsertCell();
            builder.Write("单据状态:");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(26);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);
            builder.InsertCell();
            builder.Write(purchaseReturnOrdeData.DocumentsStatus.ToString());
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(48);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);


            builder.InsertCell();
            builder.Write("");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(40);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);


            builder.InsertCell();
            builder.Write("退货仓库:");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(26);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(1);
            builder.InsertCell();
            builder.Write(purchaseReturnOrdeData.ReturnsWarehouse.ToString());
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(60);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);


            builder.EndRow();



            builder.InsertCell();
            builder.Write("审核日期:");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(26);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);
            builder.InsertCell();
            builder.Write(purchaseReturnOrdeData.AuditDate.ToString());
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(40);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);


            builder.InsertCell();
            builder.Write("");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(40);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);


            builder.InsertCell();
            builder.Write("备注:");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(26);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(1);
            builder.InsertCell();
            builder.Write(purchaseReturnOrdeData.DescInfo);
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(40);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);



            builder.EndRow();

            builder.EndTable();
        }
        /// <summary>
        /// 表体表格的 表头
        /// </summary>
        /// <param name="builder"></param>
        private void ProductProcessHead(DocumentBuilder builder)
        {
            var productTable = builder.StartTable();
            productTable.SetBorders(LineStyle.None, 0d, System.Drawing.Color.White);
            builder.CellFormat.Borders.LineStyle = LineStyle.Single;
            builder.Font.NameFarEast = "黑体";
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
            builder.Font.Size = 12;


            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;

            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
            builder.Write("序号");

            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
            builder.Write("物料编码");

            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
            builder.Write("物料名称");

            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);
            builder.Write("型号规格");

            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);
            builder.Write("主计量");


            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);
            builder.Write("退货数量");


            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);
            builder.Write("批号");



            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
            builder.Write("退货说明");


            builder.EndRow();



            builder.EndTable();

        }
        /// <summary>
        /// 表格内容
        /// </summary>
        /// <param name="builder"></param>
        private void ProductProcess(DocumentBuilder builder, PurchaseReturnOrderBackFillDto purchaseReturnOrdeData)
        {


            builder.StartTable();
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;


            foreach (var item in purchaseReturnOrdeData.purchaseReturnOrderSubtableBackFillDtos)
            {
                builder.InsertCell();
                builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
                builder.Write(item.PurchaseReturnOrderSubtableId.ToString());

                builder.InsertCell();
                builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
                builder.Write(item.MaterialNo);

                builder.InsertCell();
                builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
                builder.Write(item.MaterialName);

                builder.InsertCell();
                builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);
                builder.Write(item.TypeSpecification);

                builder.InsertCell();
                builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);
                builder.Write(item.DocumentType);


                builder.InsertCell();
                builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);
                builder.Write(item.Num.ToString());

                builder.InsertCell();
                builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);
                builder.Write(item.LotNumber);


                builder.InsertCell();
                builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
                builder.Write(item.ReturnInstructions);

                builder.EndRow();
            }


            builder.EndTable();
        }
        /// <summary>
        /// 页脚
        /// </summary>
        /// <param name="builder"></param>
        private void SignProcess(DocumentBuilder builder, PurchaseReturnOrderBackFillDto purchaseReturnOrdeData)
        {
            var signTable = builder.StartTable();
            builder.Font.Size = 12;
            builder.CellFormat.Borders.LineStyle = LineStyle.None;
            builder.RowFormat.Height = Aspose.Words.ConvertUtil.MillimeterToPoint(4);

            builder.InsertCell();
            builder.Write($"制单人：");
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(20);
            builder.InsertCell();
            builder.Write(purchaseReturnOrdeData.Operator);
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);



            builder.InsertCell();
            builder.Write("");
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(60);



            builder.InsertCell();
            builder.Write("审核人:");
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(60);
            builder.InsertCell();
            builder.Write(purchaseReturnOrdeData.Audit);
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(60);




            builder.InsertCell();
            builder.Write("");
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(60);



            builder.Write($"打印时间：");
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(20);
            builder.InsertCell();
            builder.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);

            builder.EndRow();
            builder.EndTable();
        }
    }
}
